package com.shyroke.daydayzhuanweb.controller;

import com.shyroke.daydayzhuanapi.dto.AdvertDto;
import com.shyroke.daydayzhuanapi.dto.AdvertRequest;
import com.shyroke.daydayzhuansvc.service.IAdvertService;
import com.shyroke.daydayzhuansvc.service.IOSSService;
import com.shyroke.daydayzhuansvc.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.Date;

/**
 * 类名 ：广告的controller
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2019/1/14 10:18
 */
@Controller
@RequestMapping(value = "/admin/advert")
public class AdvertController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IAdvertService advertService;

    @Autowired
    private IOSSService ossService;

    @GetMapping(value = "list")
    public ModelAndView list(ModelAndView model){

        model.setViewName("admin/project/advert_list");

        return model;
    }


    /**
     * 加载免费项目的数据
     *
     * @return
     */
    @RequestMapping(value = "getAdvertList")
    @ResponseBody
    public TabelData<AdvertDto> getAdvertList(Page page, AdvertRequest request) {
        return  advertService.getAdvertList(page,request);

    }


    /**
     * 删除广告
     * @param advert
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    @PermissionAnnotation(permissionName = "advert:delete")
    public R delete(AdvertDto advert){
        try {
            advertService.deleteById(advert.getId());
        }catch (Exception e){
            logger.info("保存失败，原因="+ExceptionUtil.getMessage(e));
            return  R.error("保存失败，原因="+e.getMessage());
        }

        return  R.ok("保存成功！");
    }


    @RequestMapping(value = "descAndEdit/{id}")
    public ModelAndView descAndEdit(@PathVariable("id")Integer id,ModelAndView model){
        AdvertDto advert =  advertService.getAdvertById(id);
        model.addObject("advert",advert);
        model.setViewName("admin/project/advert_desc_edit");
        return model;

    }

    /**
     * 保存图片
     * @param request
     * @param file
     * @return
     */
    @RequestMapping(value = "uploadImg",method = RequestMethod.POST)
    @ResponseBody
    @PermissionAnnotation(permissionName = "advert:add")
    public R uploadImg(HttpServletRequest request, @RequestParam("file")MultipartFile file){

        if(file.isEmpty()){
            return R.error("图片不能为空！");
        }

        String path = OSSClientConstants.ADVERT_PATH+file.getOriginalFilename();

        try {
            ossService.uploadImg(file,path);
        }catch (Exception e){
            logger.info(ExceptionUtil.getMessage(e));
            return  R.error("上传图片失败，原因="+e.getMessage());
        }


        return R.ok("url",path);
    }


    /**
     * 修改广告信息
     * @param advertdto
     * @return
     */
    @RequestMapping(value = "advert_update")
    @ResponseBody
    @PermissionAnnotation(permissionName = "advert:update")
    public R advert_update(@RequestBody AdvertDto advertdto){

        //防止图片地址的字符被转义，比如a(1).png会被转义成a%281%29.png,导致显示错误。
        try {
            if(advertdto.getUrl()!=null){
                advertdto.setUrl(URLDecoder.decode(advertdto.getUrl(),"utf-8"));
            }
        }catch (Exception e){
            logger.info("转义失败，原因="+ExceptionUtil.getMessage(e));
            return R.error("转义失败");
        }
        try {
            advertService.update(advertdto);
        }catch (Exception e){
            logger.info(ExceptionUtil.getMessage(e));
            return R.error("修改失败，原因="+e.getMessage());
        }
        return R.ok("修改成功！");
    }


    /**
     * 跳转广告新增页面
     * @param model
     * @return
     */
    @RequestMapping(value = "toAdd")
    public ModelAndView toAdd(ModelAndView model){
        model.setViewName("admin/project/advert_add");
        return model;
    }


    /**
     * 保存广告
     * @param advertDto
     * @return
     */
    @RequestMapping(value = "save")
    @ResponseBody
    @PermissionAnnotation(permissionName = "advert:add")
    public R save(@RequestBody AdvertDto advertDto){
        advertDto.setCreateTime(new Date());
        try {
            advertService.save(advertDto);
        }catch (Exception e){
            logger.info(ExceptionUtil.getMessage(e));
            return R.error("保存失败，原因="+e.getMessage());
        }


        return R.ok("保存成功！");
    }

}
